﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using TjZheng.Domain.DataEntity;
using TjZheng.Framework;

namespace TjZheng.Controls
{
    public class NativeUpLoad : UpLoadBase
    {
        public NativeUpLoad(UploadLimit uploadLimit)
            : base(uploadLimit)
        {

        }
        public override string SaveFile(System.IO.Stream objInputStream, string fileExtension)
        {
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), fileExtension);
            string objSavePath = HttpContext.Current.Server.MapPath(string.Format("~/filesource/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename));
            objSavePath.CreateDirectory();
            byte[] bytes = new byte[objInputStream.Length];
            objInputStream.Read(bytes, 0, bytes.Length);
            objInputStream.Seek(0, SeekOrigin.Begin);
            File.WriteAllBytes(objSavePath, bytes);
            objInputStream.Close();
            return string.Format("{0}/filesource/{1}/{2}/{3}/{4}", LoadReturnDomain(), this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
        }

        public override string SaveFile(byte[] uploadFileBytes, string fileExtension)
        {
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), fileExtension);
            string objSavePath = HttpContext.Current.Server.MapPath(string.Format("~/filesource/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename));
            objSavePath.CreateDirectory();
            File.WriteAllBytes(objSavePath, uploadFileBytes);
            return string.Format("{0}/filesource/{1}/{2}/{3}/{4}", LoadReturnDomain(), this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
        }

        public override string SaveFile(System.Web.HttpPostedFileBase objFile)
        {
            string fileExtension = Path.GetExtension(objFile.FileName);
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), fileExtension);
            string objSavePath = HttpContext.Current.Server.MapPath(string.Format("~/filesource/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename));
            objFile.SaveAs(objSavePath.CreateDirectory());
            return string.Format("{0}/filesource/{1}/{2}/{3}/{4}", LoadReturnDomain(), this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
        }

        public override string SaveFile(System.Web.HttpPostedFile objFile)
        {
            string fileExtension = Path.GetExtension(objFile.FileName);
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), fileExtension);
            string objSavePath = HttpContext.Current.Server.MapPath(string.Format("~/filesource/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename));
            objFile.SaveAs(objSavePath.CreateDirectory());
            return string.Format("{0}/filesource/{1}/{2}/{3}/{4}", LoadReturnDomain(), this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
        }

        private string LoadReturnDomain()
        {
            var Request = HttpContext.Current.Request;
            int port = Request.Url.Port;
            if (port == 80)
            {
                return string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Host);
            }
            return string.Format("{0}://{1}:{2}", Request.Url.Scheme, Request.Url.Host, port);
        }
    }
}
